Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@thi.ng/errors
Advanced tools
@thi.ng/errors is a utility library for creating and handling custom error types in JavaScript. It provides a set of predefined error classes and functions to simplify error handling and improve code readability.
Custom Error Classes
This feature allows you to create and use custom error classes like IllegalArgumentError and IllegalStateError to handle specific error conditions in your code.
const { IllegalArgumentError, IllegalStateError } = require('@thi.ng/errors');
function checkAge(age) {
if (age < 0) {
throw new IllegalArgumentError('Age cannot be negative');
}
if (age > 120) {
throw new IllegalStateError('Age seems unrealistic');
}
return 'Age is valid';
}
try {
console.log(checkAge(-5));
} catch (e) {
console.error(e.message);
}
Predefined Error Types
The package provides predefined error types like UnsupportedOperationError to handle common error scenarios in a standardized way.
const { UnsupportedOperationError } = require('@thi.ng/errors');
function notImplemented() {
throw new UnsupportedOperationError('This operation is not supported');
}
try {
notImplemented();
} catch (e) {
console.error(e.message);
}
Error Wrapping
The wrapError function allows you to wrap existing errors with additional context, making it easier to debug and understand the root cause of errors.
const { wrapError } = require('@thi.ng/errors');
function riskyOperation() {
try {
throw new Error('Something went wrong');
} catch (e) {
throw wrapError(e, 'Risky operation failed');
}
}
try {
riskyOperation();
} catch (e) {
console.error(e.message);
}
create-error-class is a utility for creating custom error classes in JavaScript. It is similar to @thi.ng/errors in that it allows for the creation of custom error types, but it does not provide predefined error types or error wrapping functionality.
verror is a library for creating and manipulating chained errors. It provides more advanced error wrapping and chaining capabilities compared to @thi.ng/errors, making it suitable for complex error handling scenarios.
custom-error-generator is a simple utility for generating custom error classes. It is similar to @thi.ng/errors in terms of creating custom error types but lacks the predefined error types and error wrapping features.
This project is part of the @thi.ng/umbrella monorepo.
Custom error types and helper fns used by many packages in this repo.
This feature was previously part of the @thi.ng/api package.
yarn add @thi.ng/errors
None
import * as err from "@thi.ng/errors";
err.illegalArity(3)
// Error: illegal arity: 3
err.illegalArgs("expected foo");
// Error: illegal argument(s): expected foo
err.illegalState("oops");
// Error: illegal state: oops
err.unsupported("TODO not yet implemented")
// Error: unsupported operation: TODO not yet implemented
© 2018 Karsten Schmidt // Apache Software License 2.0
FAQs
Custom error types and error factory functions
The npm package @thi.ng/errors receives a total of 261,528 weekly downloads. As such, @thi.ng/errors popularity was classified as popular.
We found that @thi.ng/errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.